package infra.starters;


import processing.core.*;

import org.NooLab.utilities.logging.PrintLog;



import org.NooLab.glue.components.*;



public class MessageBoardInstance  extends PApplet {

	private static final long serialVersionUID = -1253896197163588444L;

	
	BoardInstance brdInstance;
	

	
	public void setup(){
	
		background(84);
		// start this in its own thread...
		brdInstance = new BoardInstance();
	}
	
	
	
	
	public void draw(){
		 
	}
	
	public void keyPressed(){
		
		if (key=='x'){
			if (brdInstance!=null) brdInstance.stop();
			System.exit(0);
		}
	}

}

// ----------------------------------------------------------------------------


class BoardInstance implements Runnable{

	MessageBoardFactory mbfactory ;
	MessageBoardFactoryProperties factoryProperties ;
	
	
	
	Thread iThrd; 
	PrintLog out = new PrintLog(2,false);
	
	
	public BoardInstance(){
		(new Thread(this,"SrcInstance-main")).start() ;	
	}
	
	public void run() {
		
		create();
		
	}
	
	private void create(){
		
		try{
			String mbConfigPath = "D:/dev/java/data/test/mb/config/" ;
		
			factoryProperties = new MessageBoardFactoryProperties()  ;
		
			factoryProperties.setConfigPath( mbConfigPath ) ;
			 
			factoryProperties.setProtocolId( MessageBoardFactoryProperties.protocolUDP ) ;
			factoryProperties.setProtocolId( MessageBoardFactoryProperties.protocolTCP ) ;

			factoryProperties.setRemoteInstancesFlag( false ) ; // default is false
			factoryProperties.setProtocolId( MessageBoardFactoryProperties.protocolHREST );
			 
			// factoryProperties.setFactoryMode( MessageBoardFactoryProperties.__MESSAGEBOARD ) ;
			
			
			// ClientMatcher could be anything, just let it implement the ClientMatcherIntf
			// they will use the FilterPackage (if there is one)
			factoryProperties.setClientMatcher( new ClientMatcher(out)) ;

			
			// the MessageBoardFactory should be created only once 
			// it also creates just 1 MessageBoard
			// (so far... later, it will be possible to run several, e.g. one for local clients and another for remote ones)
			mbfactory = new MessageBoardFactory( factoryProperties ) ;
			
		}catch(Exception e){
			e.printStackTrace();
			System.exit(9);
		}
	}
	
	public void stop(){
		mbfactory.stopMessageBoard();
	}
	
}

